package cn.itcast.demo6.web;

import cn.itcast.demo6.mapper.PurseMapper;
import cn.itcast.demo6.pojo.OrderList;
import cn.itcast.demo6.pojo.OrderListResponse;
import cn.itcast.demo6.pojo.Purse;
import cn.itcast.demo6.service.PurseService;
import cn.itcast.demo6.utils.ApiResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Wrapper;
import java.util.List;

/**
 * ClassName: PurseController
 * Package: com.itcast.demo5.controller
 * Description:
 *
 * @Author MLH
 * @Create 2023/4/18 9:27
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/vi/order")
public class PurseController {

    @Autowired
    private PurseMapper purseMapper;

    /**
     * 接口一：查询订单所属用户的nickname
     * @param page 页面数
     * @param pers 每页记录数
     * @return
     */
    @PostMapping("/list")
    public ApiResponse getListNickName(@RequestParam("page") int page,
                                        @RequestParam("pers") int pers){
        //查询总记录数
        int total = purseMapper.getOrderCount();
        //计算总页数
        int totalpages = (int)Math.ceil((double) total / pers);
        //计算是否还有下一页
        boolean hasNext = page < totalpages;
        //搜索页面数据
        List<OrderList> data = purseMapper.getOrderList((page - 1) * pers, pers);

        OrderListResponse orderListResponse = new OrderListResponse(total, data, page, pers, hasNext);
        return new ApiResponse(10000, "操作成功", orderListResponse);
    }

    /**
     * 接口二：输出每个用户的最后一笔订单
     * @param page 页数
     * @param pers 每页记录数
     * @return
     */
    @PostMapping("endlist")
    public ApiResponse getEndListUser(@RequestParam("page") int page,
                                       @RequestParam("pers") int pers){
        //查询拥有订单用户数
        int total = purseMapper.getTotalUserCount();
        //计算总页数
        int totalpages = (int)Math.ceil((double) total / pers);
        //计算是否还有下一页
        boolean hasNext = page < totalpages;
        //搜索页面数据
        List<OrderList> data = purseMapper.getLastOrderPerUser((page - 1) * pers, pers);

        OrderListResponse orderListResponse = new OrderListResponse(total, data, page, pers, hasNext);
        return new ApiResponse(10000, "操作成功", orderListResponse);
    }




}
